import { Injectable } from '@angular/core';
import { BaseHttpService } from '../../shared/services/baseHttp.service';
import { StorageService } from '../../shared/services/storage.service';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { MostActiveReportModel } from './models/mostActiveReport.model';
import { MostRecentActivityModel } from './models/mostRecentActivity.model';
import { PortfolioListWrapper } from './models/portfolio.model';
import { NotificationsModel } from './models/notifications.model';

/**
 * Home main service
 *
 * Author: Sorin Spiru, Andrei Istrate
 * Date: 13.01.2018
 */
@Injectable()
export class HomeService extends BaseHttpService {

    constructor(public http: HttpClient){
        super();
    }

    /**
     * Get top 5 portfolio list
     */
    public getPortfolios(): Observable<PortfolioListWrapper> {
        let userInfo = StorageService.get().userInfo;

        let listOptions: any = {
            skip: 0,
            take: 5,
            orderBy: ['DateCreated'],
            orderAscending: true,
            includeAllFields: false,
            getCount: true
        };

        let params: HttpParams = new HttpParams();
        params = params.set('isMyPortfolio', 'true')
        .set('namePortfolio', '')
        .set('sharingStatus', '0')
        .set('portfolioType', '1')
        .set('isNameEqual', 'false')
        .set("userId", userInfo.userId)
        .set("accountId", userInfo.accountId);
        params = this.urlEncodeObject(listOptions, params);

        return this.http.get<PortfolioListWrapper>(this.serverUrl + 'portfolioserv/api/v1/portfolios', { params: params });
    }

    /**
     * Get most recent activity
     */
    public getMostRecentActivity(): Observable<MostRecentActivityModel[]> {
        let listOptions: any = {
            skip: 0,
            take: 5
        };

        return this.http.get<MostRecentActivityModel[]>(this.serverUrl + 'mostrecentactivityserv/api/v1/MostRecentActivity', { params: this.urlEncodeObject(listOptions)});
    }

    /**
     * Get top 20 active reports
     */
    public getTop20ActiveReports(): Observable<MostActiveReportModel[]> {
        let userInfo = StorageService.get().userInfo;

        let listOptions: any = {
            skip: 0,
            take: 5
        };

        return this.http.get<MostActiveReportModel[]>(this.serverUrl + 'marserv/api/v1/MostActiveReport', { params: this.urlEncodeObject(listOptions) });
    }

    /**
     * Get notifications
     */
    public getNotification(): Observable<NotificationsModel> {
        return this.http.get<NotificationsModel>(this.serverUrl + 'newsserv/api/v1/notifications');
    }
}